/*
*后台登陆时用到的js
*/
var COOKIE_NAME = 'sys__username';
$(function() {
	$("#login_ok").attr("disabled", false);
    choose_bg();
	//changeCode();
	if ($.cookie(COOKIE_NAME)){
	    $("#username").val($.cookie(COOKIE_NAME));
	    $("#password").focus();
	    $("#j_remember").attr('checked', true);
	} else {
		$("#username").focus();
	}
	
	
	$("#login_ok").bind("click",function(){
		if ($("#username").val()==null || $("#username").val()==""){
			//$("#msgbox").alertmsg('error', '请填写用户名', {displayMode:'slide', displayPosition:'middlecenter', okName:'确定', title:'提示'});
			$("#username").css('border-color','#ea0000')
			.css('box-shadow','0 1px 1px rgba(0,0,0,0.075) inset ,0 0 8px rgba(102 , 175 , 233 ,0.6)')
			.css('outline','0 none');
			$("#username").attr('placeholder','请输入用户名!');
			return false;
		}
		if ($("#password").val()==null || $("#password").val()==""){
			//$("#msgbox").alertmsg('error', '请填写用户名', {displayMode:'slide', displayPosition:'middlecenter', okName:'确定', title:'提示'});
			$("#password").css('border-color','#ea0000')
			.css('box-shadow','0 1px 1px rgba(0,0,0,0.075) inset ,0 0 8px rgba(102 , 175 , 233 ,0.6)')
			.css('outline','0 none');
			$("#password").attr('placeholder','请输入密码!');
			return false;
		}
		if ($("#captcha").val()==null || $("#captcha").val()==""){
			//$("#msgbox").alertmsg('error', '请填写用户名', {displayMode:'slide', displayPosition:'middlecenter', okName:'确定', title:'提示'});
			$("#captcha").css('border-color','#ea0000')
			.css('box-shadow','0 1px 1px rgba(0,0,0,0.075) inset ,0 0 8px rgba(102 , 175 , 233 ,0.6)')
			.css('outline','0 none');
			$("#captcha").attr('placeholder','请输入验证码!');
			return false;
		}
		var issubmit = true;
		var i_index  = 0;
		$(this).find('.in').each(function(i){
			if ($.trim($(this).val()).length == 0) {
				$(this).css('border', '1px #ff0000 solid');
				issubmit = false;
				if (i_index == 0)
					i_index  = i;
			}
		});
		if (!issubmit) {
			$(this).find('.in').eq(i_index).focus();
			return false;
		}
		var $remember = $("#j_remember");
		if ($remember.attr('checked')) {
			$.cookie(COOKIE_NAME, $("#username").val(), { path: '/', expires: 15 });
		} else {
			$.cookie(COOKIE_NAME, null, { path: '/' });  //删除cookie
		}
		$("#login_ok").attr("disabled", true).val('登陆中..');
		var password = HMAC_SHA256_MAC($("#username").val(), $("#password").val());
		$.ajax({
			url: "admin/login",
			type: "POST",
			data:$('#login_form').serialize(),
			success: function(data) {
				if (data.success){
					if (data.state == 0){
						window.location.href="admin/info";
					} 
				}else{
					if(data.state == 1){
						$("#captcha").val("");
						$("#captcha").css('border-color','#ea0000')
						.css('box-shadow','0 1px 1px rgba(0,0,0,0.075) inset ,0 0 8px rgba(102 , 175 , 233 ,0.6)')
						.css('outline','0 none');
						$("#captcha").attr('placeholder','验证码错误!');						
					}else if(data.state == 2){
						$("#username").val("");
						$("#password").val("");
						$("#captcha").val("");
						$("#username").css('border-color','#ea0000')
						.css('box-shadow','0 1px 1px rgba(0,0,0,0.075) inset ,0 0 8px rgba(102 , 175 , 233 ,0.6)')
						.css('outline','0 none');
						$("#username").attr('placeholder','用户名密码错误!');
						$(this).attr("src" , "admin/captcha?tt="+Math.random());
					}
					$("#login_ok").attr("disabled", false).val(' 登  陆  ');
				}
			}
		});
		return false;//避免submit自动提交
	});
	
	$("#captcha_img").bind("click" , function(){
		$(this).attr("src" , "admin/captcha?tt="+Math.random());
	});
	
	$("#username").focus(function(){
		$(this).attr('placeholder','');
		$(this).css('border-color','#66afe9')
		.css('box-shadow','0 1px 1px rgba(0,0,0,0.075) inset ,0 0 8px rgba(102 , 175 , 233 ,0.6)')
		.css('outline','0 none');
	});
	$("#password").focus(function(){
		$(this).attr('placeholder','');
		$(this).css('border-color','#66afe9')
		.css('box-shadow','0 1px 1px rgba(0,0,0,0.075) inset ,0 0 8px rgba(102 , 175 , 233 ,0.6)')
		.css('outline','0 none');
	});
	
	$("#username").blur(function(){
		$(this).css('border','1px solid #ccc')
		.css('box-shadow','inset 0 1px 1px rgba(0, 0, 0, .075)');
	});
	
	$("#password").blur(function(){
		$(this).css('border','1px solid #ccc')
		.css('box-shadow','inset 0 1px 1px rgba(0, 0, 0, .075)');
	});
	
	$("#captcha").focus(function(){
		$(this).attr('placeholder','');
		$(this).css('border-color','#66afe9')
		.css('box-shadow','0 1px 1px rgba(0,0,0,0.075) inset ,0 0 8px rgba(102 , 175 , 233 ,0.6)')
		.css('outline','0 none');
	});
	$("#captcha").blur(function(){
		$(this).css('border','1px solid #ccc')
		.css('box-shadow','inset 0 1px 1px rgba(0, 0, 0, .075)');
	});
});
function genTimestamp(){
	var time = new Date();
	return time.getTime();
}
function changeCode(){
	//$("#captcha_img").attr("src", "/captcha.jpeg?t="+genTimestamp());
}
function choose_bg() {
	var bg = Math.floor(Math.random() * 9 + 1);
	$('body').css('background-image', 'url(static/images/loginbg_0'+ bg +'.jpg)');
}